﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Globalization;

namespace Mindfor.Web
{
	/// <summary>
	/// Provides extension functions for ModelStateDictionary.
	/// </summary>
	public static class ModelStateDictionaryExtensions
	{
		/// <summary>
		/// Sets the value for the specified key if one does not exists.
		/// </summary>
		/// <param name="modelState">ModelState.</param>
		/// <param name="key">The key.</param>
		/// <param name="value">The value.</param>
		public static void SetDefaultModelValue(this ModelStateDictionary modelState, string key, object value)
		{
			if (modelState[key] == null)
			{
				string attemptedValue = value == null ? null : value.ToString();
				modelState.SetModelValue(key, new ValueProviderResult(value, attemptedValue, CultureInfo.CurrentUICulture));
			}	
		}
	}
}
